package com.xb.cloud.order.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.order.model.SoExample;
import com.xb.cloud.order.model.SoExample.Criteria;
import com.xb.cloud.order.model.mapper.SellOrderMapper;
import com.xb.cloud.order.service.client.StatementService;
import com.xb.cloud.order.service.client.bo.StatementTradeInfo;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class StatementServiceImpl implements StatementService {

	@Resource
	SellOrderMapper sellOrderMapper;

	@Override
	public ServiceReturnInfo getMerchantFinaceInfo(EndMerchantUserBo merchantUserBo) {
		Byte accountType = merchantUserBo.getAccountType();

		Long companyId = merchantUserBo.getCompanyId();
		Long merchantId = merchantUserBo.getMerchantId();
		Long shopId = merchantUserBo.getShopId();

		SoExample example = new SoExample();

		Criteria createCriteria = example.createCriteria();
		if (DbConstants.ACCOUNT_TYPE_TOP_MERCHANT == accountType
				|| DbConstants.ACCOUNT_TYPE_LEAF_MERCHANT == accountType) {
			// 商家用户
			createCriteria.andCompanyIdEqualTo(companyId).andMerchantIdEqualTo(merchantId);
		}

		if (DbConstants.ACCOUNT_TYPE_SHOP == accountType) {
			// 店铺用户
			createCriteria.andCompanyIdEqualTo(companyId).andMerchantIdEqualTo(merchantId).andShopIdEqualTo(shopId);
		}

		List<Integer> status = Arrays.asList(DbConstants.SO_STATUS_ALREADY_PAY,
				DbConstants.SO_STATUS_ALREADY_COMPLETED);
		createCriteria.andSoStatusIn(status);

		BigDecimal decimal = sellOrderMapper.getMerchantSellTotalAmountWithStatus(example);

		return ServiceReturnUtils.success(decimal);
	}

	@Override
	public ServiceReturnInfo getTradeInfo(EndMerchantUserBo merchantUserBo, Date startTimeString, Date endTimeString) {
		StatementTradeInfo tradeInfo = new StatementTradeInfo();
		
		SoExample example = new SoExample();
		Criteria criteria = example.createCriteria();
		criteria.andCreateTimeBetween(startTimeString, endTimeString);
		criteria.andShopIdEqualTo(merchantUserBo.getShopId());
		//50-已完成 
		criteria.andSoStatusEqualTo(50);
		//1:成交金额
		BigDecimal total_amount = sellOrderMapper.getTotalSellAmount(example);
		if(total_amount == null ) {
			return  ServiceReturnUtils.success(tradeInfo);
		}
		tradeInfo.setTotal_amount(total_amount);
		//2:成交订单数
		Integer order_count = sellOrderMapper.getOrderCount(example);
		tradeInfo.setOrder_count(order_count);
		//3:线上支付金额
		criteria.andPaymentTypeEqualTo((byte)1);
		BigDecimal payment_online = sellOrderMapper.paymentAmountByType(example);
		if(payment_online != null) {
			tradeInfo.setPayment_online(payment_online);
		}
		
		example.clear();
	    criteria = example.createCriteria();
		criteria.andCreateTimeBetween(startTimeString, endTimeString);
		criteria.andShopIdEqualTo(merchantUserBo.getShopId());
		criteria.andSoStatusEqualTo(50);
		
		criteria.andPaymentTypeEqualTo((byte)2);
		//4:线下支付金额
		BigDecimal payment_offline = sellOrderMapper.paymentAmountByType(example);
		if(payment_offline != null) {
			tradeInfo.setPayment_offline(payment_offline);
		}
		//5:客单价
		double per_Customer_Transaction = total_amount.doubleValue()/order_count.doubleValue();
		per_Customer_Transaction = (double) Math.round(per_Customer_Transaction * 100) / 100;
		tradeInfo.setPer_Customer_Transaction(per_Customer_Transaction);
		
		example.clear();
	    criteria = example.createCriteria();
		criteria.andSoCreateTimeBetween(startTimeString, endTimeString);
		criteria.andSoShopIdEqualTo(merchantUserBo.getShopId());
		criteria.andSoStatusEqualTo(50);
		//6:成交件数
		Integer sku_count = sellOrderMapper.getSkuCount(example);
		tradeInfo.setSku_count(sku_count);
		
		//7:成交买家数
		Integer buy_count = sellOrderMapper.getByCount(example); 
		tradeInfo.setBuy_count(buy_count);
		
		example.clear();
	    criteria = example.createCriteria();
		criteria.andCreateTimeBetween(startTimeString, endTimeString);
		criteria.andShopIdEqualTo(merchantUserBo.getShopId());
		//8:订单成交概率
		Integer order_all_count = sellOrderMapper.getOrderCount(example);
		double so_Transaction_Rate = order_count.doubleValue()/order_all_count;
		so_Transaction_Rate = (double) Math.round(so_Transaction_Rate * 100) / 100;
		tradeInfo.setSo_Transaction_Rate(so_Transaction_Rate);
		
		return  ServiceReturnUtils.success(tradeInfo);
	}

}
